package com.example.myretrofit2.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.myretrofit2.Category;
import com.example.myretrofit2.R;
import com.example.myretrofit2.activity.GoodsList;
import com.example.myretrofit2.adapter.CategoryLeftAdapter;
import com.example.myretrofit2.adapter.CategoryRightAdapter;
import com.example.myretrofit2.adapter.OnitemClickListener;
import com.example.myretrofit2.common.MyApplication;
import com.example.myretrofit2.presenter.CategoryPresenter;
import com.example.myretrofit2.presenter.MySubscriber;

import java.util.List;


public class CategoryFragment extends Fragment {
ImageView titleBack;
RecyclerView leftList;
RecyclerView rightList;
CategoryRightAdapter categoryRightAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_category, container, false);
        initView(view);
        initData();
        return view;
    }
    private void initView(View view){
        titleBack = view.findViewById(R.id.title_back);
        titleBack.setVisibility(View.GONE);//隐藏视图控件信息，不会占据空间大小
        leftList = view.findViewById(R.id.left_list);
        rightList = view.findViewById(R.id.right_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MyApplication.getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        leftList.setLayoutManager(linearLayoutManager);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(MyApplication.getContext(),3);
        rightList.setLayoutManager(gridLayoutManager);
    }
    public void initData(){
        CategoryPresenter.getCategory(new MySubscriber<List<Category>>(MyApplication.getContext()) {
            @Override
            public void onNext(List<Category> categories) {
                CategoryLeftAdapter categoryLeftAdapter = new CategoryLeftAdapter(categories,MyApplication.getContext());
                categoryLeftAdapter.setListener(new OnitemClickListener() {
                    @Override
                    public void onItemClick(Category category) {
                         categoryRightAdapter = new CategoryRightAdapter(category.getChildList(),MyApplication.getContext());
                        rightList.setAdapter(categoryRightAdapter);
                        categoryRightAdapter.setListener(new OnitemClickListener() {
                            @Override
                            public void onItemClick(Category catagory) {
                                Intent intent= new Intent();
                                intent.putExtra("catagoryId",catagory.getId());
                                intent.setClass(getActivity(), GoodsList.class);
                                startActivity(intent);
                            }
                        });
                    }
                });
                leftList.setAdapter(categoryLeftAdapter);
                categoryRightAdapter = new CategoryRightAdapter(categories.get(0).getChildList(),MyApplication.getContext());
                rightList.setAdapter(categoryRightAdapter);
                categoryRightAdapter.setListener(new OnitemClickListener() {
                    @Override
                    public void onItemClick(Category catagory) {
                        Intent intent= new Intent();
                        intent.putExtra("catagoryId",catagory.getId());
                        intent.setClass(getActivity(), GoodsList.class);
                        startActivity(intent);
                    }
                });
            }
        });
    }
}